var vm = new Vue({
    //vue对象节点,指向父盒子
    el:'#user',                                                                   
    //vue实例 的data属性
    data:function(){
    	var validatePass2 = function(rule, value, callback){
            if (value === '') {
              callback(new Error('请再次输入密码'));
            } else if (value !== vm.user.password) {
              callback(new Error('两次输入密码不一致!'));
            } else {
            	
              callback();
            }
        };
        var validatePass = function(rule, value, callback){
            if ($.trim(value) === '') {
              callback(new Error('请输入用户名'));
            } else if ($.trim(value).length >15) {
              callback(new Error('用户名长度不能超过15'));
            } else {
               var url = "./isUserExist.action";
         	   var params = {
         		   'user.userCode':$.trim(value)
         	   }
         	   pb.ajaxPost(url,params,function(r){
     	       		if(r.isUserExist == true){
     	       		callback(new Error('用户名不可重复'));
     	       		}else{callback();}
     	       });
            }
        };
        return{
          pageA:true,
          pageB:false,
          pageC:false,
          userCode:'',
          userName:'',
          createDate:[],
	      tableData:[],
	      rowS:{
	    	  height:30
	      },
	      multipleSelection:[],
	      //分页条
	      pageSize:10,
	      currPage:1,
	      total:1,
	      //
	      isUserCode:false,
	      user:{},
	      typeList:[{value: '0', label: '普通用户'},
              		{ value: '1',label: '管理员'},
          ],
          statusList:[{value: '1', label: '正常'},
        		    { value: '2',label: '禁用'},
          ],
	      userRules:{ 
	    	  userCode: [{ required: true, message: '请输入用户名', trigger: 'blur' },
	    		  		{validator: validatePass, trigger: 'blur'}],
		   	  password: [{ required: true, message: '请输入密码', trigger: 'blur' },
		   	     { max: 15, message: '长度不能超过15个字符', trigger: 'blur' }],
		   	  rePassword: [{ required: true, message: '请再次输入密码', trigger: 'blur' },
		   		           {validator: validatePass2, trigger: 'blur'},
			   	           { max: 15, message: '长度不能超过15个字符', trigger: 'blur' }],
			  tel: [{ required: true, message: '请输入手机号', trigger: 'blur' },
			   	    { type: 'number', message: '必须为数字值'}],
			  email: [{ required: true, message: '请输入邮箱', trigger: 'blur' },
			   	   	 {type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur,change'}],
		   	  type: [{ required: true, message: '请选择用户类型', trigger: 'blur，change' }],
		   	  status: [{ required: true, message: '请选择用户状态', trigger: 'blur，change' }]
	      }
        };
    },
    //vue实例 的methods
    methods:{
       handleSizeChange:function(val) {
          vm.pageSize = val;
          vm.query();
       },
       handleCurrentChange:function(val) {
    	   vm.currPage = val;
    	   vm.query();
       },
       handleSelectionChange(val) {
           vm.multipleSelection = val;
       },
       //格式化日期
       dateFormat:function(row,column){
    	   var date = row[column.property];
    	   if(date == undefined){
    		   return "";
    	   }
    	   return moment(date).format("YYYY-MM-DD HH:mm:ss");
       },
       loadData:function(){
    	   var url = "./listUser.action";
    	   if(vm.createDate == null || vm.createDate.length ==0 
    			   || typeof(vm.createDate[0]) == 'string' || typeof(vm.createDate[1]) == 'string'){
    		   vm.createDate = ['',''];
		   }else{
			   vm.createDate.splice(0,2,moment(vm.createDate[0]).format('YYYY-MM-DD'),
			   moment(vm.createDate[1]).add(1, 'days').format('YYYY-MM-DD'));
		   };
    	   var params = {
    		   'pagemap.size':vm.pageSize,
    		   'pagemap.page':vm.currPage,
    		   'pagemap.userCode':vm.userCode,
    		   'pagemap.userName':vm.userName,
    		   'pagemap.mindate':vm.createDate[0],
    		   'pagemap.maxdate':vm.createDate[1]
    	   }

    	   pb.ajaxPost(url,params,function(r){
	       		if(r.code == '000000'){
	       			vm.tableData = r.userList;
	       			vm.total = r.usercount;
	       		}else{
	       			pb.alertErr('加载失败');
	       		}
	       });
       },
       query:function(){
    	   vm.loadData();
       },
       getUser:function(){
    	   var url = "./getUserById.action";
    	   var params = {
    		   'user.id':vm.multipleSelection[0].id
    	   }
    	   pb.ajaxPost(url,params,function(r){
	       		if(r.code == '000000'){
	       			vm.user = r.user;
	       			vm.user.tel = parseInt(vm.user.tel);
	       		}else{
	       		 pb.alertErr('获取用户信息失败');
	       		}
	       },function(err){
	    	   pb.alertErr('获取用户信息失败');
	       });
       },
       checkUserCode:function(){
    	   var url = "./isUserExist.action";
    	   var params = {
    		   'user.userCode':vm.user.userCode
    	   }
    	   pb.ajaxPost(url,params,function(r){
	       		if(r.isUserExist == 'true'){
	       			return true;
	       		}else{return false}
	       });
       },
       //新增
       add:function(){
    	   vm.pageA=false;
    	   vm.pageB=true;
    	   vm.pageC=false;
    	   vm.isUserCode= false;
       },
       //更新
       update:function(){
    	   if(vm.multipleSelection == null || vm.multipleSelection.length !=1){
    		   pb.alertWar("请选择一条记录");
    		   return false;
    	   }
    	   vm.pageA=false;
    	   vm.pageB=true;
    	   vm.pageC=false;
    	   vm.isUserCode= true;
    	   vm.getUser();
       },
       //删除
       del:function(){
    	   if(vm.multipleSelection == null || vm.multipleSelection.length <=0){
    		   pb.alertWar("请选择至少一条记录");
    		   return false;
    	   }
    	   var ids = [];
    	   for(var i=0;i<vm.multipleSelection.length;i++){
    		   ids.push(vm.multipleSelection[i].id);
    	   }
    	   
    	   vm.$confirm('该操作也将删除该用户下的所有发帖，确定删除么?','提示',{
               confirmButtonText: '确定',
               cancelButtonText: '取消',
               type:'warning',
               callback:function(e){
                   if(e === 'confirm'){
                	   var params = {
                		   "res.ids":JSON.stringify(ids)
                		   
                			//  "user.id":vm.multipleSelection[0].id
                	   }
                       pb.ajaxPost("./deleteUser.action",params,function(res){
                           if(res.code == '000000'){
                        	   pb.alertSuc('删除成功');
                               vm.query();
                           }else{
                           	pb.alertErr('删除失败');
                           }
                       });
                   	
                   }
               }
           });
       },
       submit:function(formName){
    	   vm.$refs[formName].validate(function(valid) {
 	          if (valid) {
 	        	  var url ="";
 	        	  if(vm.user.id == null || vm.user.id == ''){
 	        		 url = './save.action';
 	        	  }else{
 	        		 url = './updateUser.action';
 	        	  }
                  
                  var params = {
                		"user.id":vm.user.id,
                  		"user.userCode":vm.user.userCode,
                  		"user.userName":vm.user.userName,
                  		"user.password":vm.user.password,
                  		"user.tel":vm.user.tel,
                  		"user.email":vm.user.email,
                  		"user.type":vm.user.type,
                  		"user.status":vm.user.status,		
                  };
                  
                  pb.ajaxPost(url,params,function(r){
                  	if(r.code == '000000'){
                  		pb.alertSuc("提交成功");
                  		vm.back('userRef');
                  	}else{
                  		pb.alertErr("提交失败");
                  	}
                  	},function(err){

                  		pb.alertErr("操作超时");
                  	});
 	          } else {
 	            return false;
 	          }
 	    });
       },
       back:function(formName){
    	   vm.pageA=true;
    	   vm.pageB=false;
    	   vm.pageC=false;
    	   vm.user = {};
    	   vm.isUserCode= false;
    	   vm.$refs[formName].resetFields();
    	   vm.loadData(vm.pageSize,vm.currPage);
       }


    }
    //vue实例 的methods结束
});
vm.loadData(10,1);